import Vue from 'vue'
//时间戳转换成时间
export const timestampToTime = (value,type) => {
	if(value==0){
		return value;
	}
	var date = '';
	var lenth = (value + '').length;
	value = parseInt(value);
	if (lenth == 10) date = new Date(value * 1000);
	else if (lenth == 13) date = new Date(value);
	var Y = date.getFullYear()
	var M = '-' + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
	var D = '-' + (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
	var h = ' ' + (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours())
	var m = ':' + (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes())
	var s = ':' + (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds())
	if(type=='year'){
		return Y;
	}else if(type=='mouth'){
		return Y + M;
	}else if(type=='day'){
		return Y + M + D;
	}else if(type=='hour'){
		return Y + M + D + h;
	}else if(type=='minute'){
		return Y + M + D + h + m;
	}else if(type=='describe'){
		return Y + M + D + h + m + s;
	}
}
//截取地址栏参数
export const getUrl = (key) => {
	var url = location.href; //首先获取url
	if (url.indexOf("?") != -1) { //判断是否有参数
		var strSub = null;
		var str = url.split("?"); //根据？截取url
		var strs = str[1].split("&"); //str[1]为获取？号后的字符串，并且根据&符号进行截取，得到新的字符串数组，这个字符串数组中有n个key=value字符串
		for (var i = 0; i < strs.length; i++) { //遍历字符串数组
			strSub = strs[i].split("="); //取第i个key=value字符串，并用“=”截取获得新的字符串数组 这个数组里面第0个字符是key，第1个字符value
			if (strSub[0] == key) { //判断第0个字符与该方法的参数key是否相等，如果相等 则返回对应的值value。
				return strSub[1];
			}
		}
	}
	return "";
}

// 函数防抖
export const debounce = (fn, t) =>{
	let delay = t || 1000;
	let timer;
	return function () {
	    let args = arguments;
	    if (timer) clearTimeout(timer);
	    let callNow = !timer;
	    timer = setTimeout(() => {
	      timer = null;
		  // fn.apply(this, args);//这段代码只执行时间段内的最后一次点击
	    }, delay)
	
	    if (callNow) fn.apply(this, args) //这段代码只执行时间段内的第一次
	}
}
// 函数节流
export const throttle = (fn, t) =>{
	let last;
	let timer;
	let interval = t || 500;
	return function () {
	    let args = arguments;
	    let now = +new Date();
	    if (last && now - last < interval) {
	      clearTimeout(timer);
	      timer = setTimeout(() => {
	        last = now;
	        fn.apply(this, args);
	      }, interval);
	    } else {
	      last = now;
	      fn.apply(this, args);
	    }
	}
}
// 金额单位换算，分改为元
export const priceInteger = (price) => {
	price=parseInt(price)
	return (price/100).toFixed(2);
}
// 金额单位换算，分改为元
export const priceIntegernew = (price) => {
	price=parseInt(price)
	return (price/100).toFixed(1);
}